package org.lemon.transport.context.service;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.lemon.transport.context.ota.OtaPackageDataCache;
import org.lemon.transport.context.service.discovery.ServiceInfoProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Data
@Component
public class TransportContext {

    @Autowired
    private TransportService transportService;

    @Autowired
    private ServiceInfoProvider serviceInfoProvider;

    @Autowired
    private OtaPackageDataCache otaPackageDataCache;

    @Value("${transport.executor.pool_size:2}")
    private Integer poolSize;

    @Autowired
    private SchedulerComponent scheduler;

    private ExecutorService executor;

    private String serviceId;

    @PostConstruct
    public void init() {
        this.serviceId = UUID.randomUUID().toString();
        this.executor = Executors.newWorkStealingPool(this.poolSize);
    }

    public String getServiceId() {
        return serviceInfoProvider.getServiceId();
    }

    public ExecutorService getExecutor() {
        return executor;
    }
}
